import axios from "axios";

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

// 创建axios实例
const mock = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: "http://localhost:8080",
  // 超时
  timeout: 10000,
  headers: {
    "Content-Type": "application/json;charset=utf-8"
  }
});

mock.interceptors.request.use(
  function(config) {
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

mock.interceptors.response.use(
  function(config) {
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

export function mockdata(params) {
  return new Promise((resolve, reject) => {
    mock({
      url: params.url,
      method: params.method || "GET",
      data: params.data
    })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err);
      });
  });
}
